package top.wardyan.wdev.dbassist.action.dic;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import top.wardyan.wdev.dbassist.action.base.BaseDialog;
import top.wardyan.wdev.dbassist.action.base.BaseShell;
import top.wardyan.wdev.dbassist.config.DDA;
import top.wardyan.wdev.dbassist.model.YTDic;
import top.wardyan.wdev.dbassist.model.YTDicItem;

public class DicShell extends BaseShell<DicShell>{
	protected Composite mainCom;
	protected Composite colCom;
	protected Table table;
	protected Table colTable;
	protected Label mainInfo;
	protected Text mainSearch;
	protected Button btnMainAdd;
	protected Button btnDetailAdd;
	
	
	private void resize() {
		mainCom.setBounds(0, topCom.getLocation().y + topCom.getSize().y + 2, self.getSize().x / 2, self.getSize().y - topCom.getLocation().y - topCom.getSize().y - 2);
		mainSearch.setBounds(0, 0, mainCom.getSize().x - 80, 30);
		btnMainAdd.setBounds(mainSearch.getLocation().x + mainSearch.getSize().x + 5, 0, 70, 30);
		// table.setBounds(0, 45, mainCom.getSize().x, mainCom.getSize().y - 75);
		table.setBounds(0, 35, mainCom.getSize().x, mainCom.getSize().y - 65);
		
		//colTable.setBounds(700, 100, 1000, 300);
		colCom.setBounds(mainCom.getLocation().x + mainCom.getSize().x + 5, mainCom.getLocation().y, this.getSize().x - mainCom.getSize().x - mainCom.getLocation().x - 10, mainCom.getSize().y);
		colTable.setBounds(0, 0, colCom.getSize().x, colCom.getSize().y - 30);
	}
	public DicShell() {
		super();
		//setSize(1024, 720);
		createContents();
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				resize();
			}
		});
		this.resize();
	}
	
	private List<YTDic> dics = null;
	
	
	private void createContents() {
		mainCom = new Composite(this, SWT.BORDER);
		mainCom.setLocation(0, topCom.getLocation().y + topCom.getSize().y + 2);
		/**
		mainInfo = new Label(mainCom, SWT.NONE);
		mainInfo.setBounds(0, 0, 100, 30);
		mainInfo.setText("");
		**/
		mainSearch = new Text(mainCom, SWT.NONE);
		mainSearch.setBounds(0, 0, 100, 30);
		//mainSearch.setToolTipText("");
		mainSearch.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				self.list(false);
			}
		});
		btnMainAdd = new Button(mainCom, SWT.NONE);
		btnMainAdd.setBounds(100, 0, 100, 30);
		btnMainAdd.setText("新增");
		btnMainAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new BaseDialog(self, new DicAddShell()).open();
				self.list(true);
			}
		});
		
		table = new Table(mainCom, SWT.NONE);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = table.getSelectionIndex();
				if(index >= 0) {
					TableItem item = table.getItem(index);
					String objectID = item.getText(0);
					self.show(Integer.valueOf(objectID));
				}
			}
		});
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = table.getSelectionIndex();
				//System.out.println(index);
				if(index >= 0) {
					String str = table.getItem(index).getText(0);
					if(str != null && str.length() > 0) {
						// new DicEditShell(Integer.valueOf(str)).open();
						new BaseDialog(self, new DicEditShell(Integer.valueOf(str))).open();
					}
					self.list(true);
				}
				
			}
		});
		
		TableColumn col = new TableColumn(table, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("sID");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(150);
		col.setAlignment(SWT.LEFT);
		col.setText("名称");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("字段名1");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("字段名2");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("字段名3");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(150);
		col.setAlignment(SWT.LEFT);
		col.setText("备注");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("状态");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(150);
		col.setAlignment(SWT.LEFT);
		col.setText("创建时间");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(150);
		col.setAlignment(SWT.LEFT);
		col.setText("最后修改");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("版本");
		
		
		// List<Record> rs = this.list();
		//dics = DDA.getInstance().dicService.list();
		//System.out.println(rs.size());
		list(true);
		
		
		//TableItem item = new TableItem(table, SWT.NONE);
		colCom = new Composite(this, SWT.NONE);
		colCom.setBounds(mainCom.getLocation().x + mainCom.getSize().x + 5, mainCom.getLocation().y, this.getSize().x - mainCom.getSize().x - mainCom.getLocation().x - 5, mainCom.getSize().y);
		
		colTable = new Table(colCom, SWT.NONE);
		colTable.setHeaderVisible(true);
		colTable.setLinesVisible(true);
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("sIndex");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("取值");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(200);
		col.setAlignment(SWT.LEFT);
		col.setText("名称");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("说明");
		
	}
	
	public void list(boolean force) {
		table.removeAll();
		if(force) {
			dics = DDA.getInstance().dicService.list();
		}
		if(dics != null) {
			for(YTDic r : dics) {
				if(mainSearch.getText().equals("") || r.getXName().contains(mainSearch.getText()) || r.getXField().contains(mainSearch.getText())
						|| r.getXField2().contains(mainSearch.getText()) || r.getXField3().contains(mainSearch.getText()) || r.getXNote().contains(mainSearch.getText())) {
					TableItem item = new TableItem(table, SWT.NONE);
					item.setText(new String[] {
							r.getStr("sID"), 
							r.getStr("xName"), 
							r.getStr("xField"), 
							r.getStr("xField2"), 
							r.getStr("xField3"), 
							r.getStr("xNote"), 
							r.getStr("sStatus"), 
							r.getStr("sCreateTime"), 
							r.getStr("sLastUpdate"), 
							r.getStr("sVersion")
					});
				}
			}
		}
	}
	
	private void show(Integer mID) {
		//List<Record> rs = Db.find("select m.*, t.name as typename, n.* from sys.columns as m inner join sys.tables as b on b.object_id = m.object_id left join sys.types as t on t.system_type_id = m.system_type_id left join dbo.yt_table_col as n on n.xMainName = b.name where m.object_id = ?", objectID);
		//System.out.println(rs.size());
		List<YTDicItem> ds = DDA.getInstance().dicItemService.list(mID);
		colTable.removeAll();
		if(ds != null && ds.size() > 0) {
			for(YTDicItem r : ds) {
				TableItem item = new TableItem(colTable, SWT.NONE);
				item.setText(new String[] {
						r.getStr("sIndex"),
						r.getStr("xValue"),
						r.getStr("xName"),
						r.getStr("xNote")
				});
			}
		}
	}
	
	
	public static void main(String[] args) {
		new DicShell().go();
	}

	@Override
	protected String title() {
		return "数据字典";
	}
}
